﻿using Heima.OA.Common;
using Heima.OA.IBLL;
using Heima.OA.Model;
using Heima.OA.UI.Portal.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Heima.OA.UI.Portal.Areas.Check.Controllers
{
    public class CheckInfoController : BaseController
    {
        public IZS_CheckInfoService ZS_CheckInfoService { get; set; }
        public IZS_CheckBuildingInfoService ZS_CheckBuildingInfoService { get; set; }
        public IZS_LogService ZS_LogService { get; set; }
        
        // GET: Check/CheckInfo
        public string Index(PaginationQuery paginationQuery)
        {
            int total = 0;
            var checkInfoList = ZS_CheckInfoService.GetPageEntities(paginationQuery.pagesize, paginationQuery.pageindex, out total, u => u.DelFlag == delflagNormal,
                s => s.CreateTime, true);
            //页面返回多张表的关联数据
            var objList = ZS_CheckInfoService.GetIndexList(checkInfoList.ToList());
            PaginationData data = new PaginationData() { total = total, rows = JsonHandler.ToJson(objList) };
            JsonMessage msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "", JsonHandler.ToJson(data));

            return JsonHandler.ToJson(msg);
        }

        #region 添加
        public string Create(string checkInfoView)
        {
            JsonMessage msg = new JsonMessage();
            try
            {
                CheckInfoView view = JsonHandler.Deserialize<CheckInfoView>(checkInfoView);
                ZS_CheckInfo checkInfo = new ZS_CheckInfo();
                checkInfo.JingbanPerson = view.JingbanPerson;
                checkInfo.CheckPerson = view.CheckPerson;
                checkInfo.CheckDate = view.CheckDate;
                checkInfo.FamilyId = view.FamilyId;
                checkInfo.BuildingId = view.BuildingId;
                checkInfo.DelFlag = delflagNormal;
                checkInfo.CreateTime = DateTime.Now;
                checkInfo.CreateOperator = LoginUser.UName;
                checkInfo.CreateOperatorId = LoginUser.Id;
                ZS_CheckInfoService.Add(checkInfo);
                if (checkInfo.Id > 0)
                {
                    if (view.CheckHouseList.Count > 0)
                    {
                        ZS_CheckInfoService.AddCheckHouse(checkInfo.Id, checkInfo.BuildingId??0, view.CheckHouseList);
                    }
                    if (view.BusinessList.Count > 0)
                    {
                        ZS_CheckInfoService.AddPrivateBusiness(checkInfo.Id, checkInfo.BuildingId ?? 0, view.BusinessList);
                    }
                    if (view.ResettlementList.Count > 0)
                    {
                        ZS_CheckInfoService.AddResettlement(checkInfo.Id, checkInfo.FamilyId ?? 0, view.ResettlementList);
                    }
                    msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "添加成功!");
                }
                else
                {
                    msg = JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "添加失败!");
                }
            }
            catch(Exception ex)
            {
                msg = JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "添加异常!"+ex.Message);
            }

            return JsonHandler.ToJson(msg);
        }
        #endregion
        #region 编辑
        public string Edit(string checkInfoView)
        {
            JsonMessage msg = new JsonMessage();
            try
            {
                CheckInfoView view = JsonHandler.Deserialize<CheckInfoView>(checkInfoView);
                ZS_CheckInfo checkInfo = ZS_CheckInfoService.GetModel(view.Id);
                if(checkInfo == null)
                {
                    msg = JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "编辑失败!");
                }
                else
                {
                    //checkInfo.JingbanPerson = view.JingbanPerson;
                    //checkInfo.CheckPerson = view.CheckPerson;
                    //checkInfo.CheckDate = view.CheckDate;
                    checkInfo.FamilyId = view.FamilyId;
                    checkInfo.BuildingId = view.BuildingId;
                    ZS_CheckInfoService.Update(checkInfo);
                    if (checkInfo.Id > 0)
                    {
                        if (view.CheckHouseList.Count > 0)
                        {
                            ZS_CheckInfoService.AddCheckHouse(checkInfo.Id, checkInfo.BuildingId ?? 0, view.CheckHouseList);
                        }
                        if (view.BusinessList.Count > 0)
                        {
                            ZS_CheckInfoService.AddPrivateBusiness(checkInfo.Id, checkInfo.BuildingId ?? 0, view.BusinessList);
                        }
                        if (view.ResettlementList.Count > 0)
                        {
                            ZS_CheckInfoService.AddResettlement(checkInfo.Id, checkInfo.FamilyId ?? 0, view.ResettlementList);
                        }
                        msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "编辑成功!");
                    }
                    else
                    {
                        msg = JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "编辑失败!");
                    }
                }
            }
            catch (Exception ex)
            {
                msg = JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "编辑异常!" + ex.Message);
            }

            return JsonHandler.ToJson(msg);
        }
        #endregion
        #region 删除
        public string Delete(int id)
        {
            ZS_CheckInfo checkInfo = ZS_CheckInfoService.GetModel(id);
            string res = string.Empty;
            if (checkInfo == null)
            {
                res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "删除错误!"));
            }
            else
            {
                checkInfo.DelFlag = (int)DelFlagEnum.Deleted;
                bool success = ZS_CheckInfoService.Update(checkInfo);
                if (success)
                {
                    ZS_LogService.AddLog((int)AllEnum.BusinessTypeEnum.申报, checkInfo.Id, "删除核查表信息", LoginUser.UName, LoginUser.Id);
                    res = JsonHandler.ToJson(JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "删除成功!"));
                }
            }
            return res;
        }
        #endregion
        #region 通过申报家庭id 获取申报家庭人员 申报的所有房子 转换为核查的房子
        public string GetCheckHouseByFamilyId(int DeclareFamilyId)
        {
            JsonMessage msg = new JsonMessage();
            List<ZS_CheckHouseInfo> checkHouseList = ZS_CheckInfoService.GetCheckHouseList(DeclareFamilyId);
            List<object> objList = new List<object>();
            foreach(var house in checkHouseList)
            {
                ZS_CheckBuildingInfo buildingInfo = ZS_CheckBuildingInfoService.GetModel(house.CheckBuildingId??0);
                objList.Add(new
                {
                    house.Id,
                    house.DeclareHouseId,
                    house.CheckBuildingId,
                    BuildingName = buildingInfo!=null ?  buildingInfo.BuildingName : "",
                    house.HouseName,
                    house.HouseArea,
                    house.DecorationSituation,
                    house.ActualityUse,
                    house.Remark,
                    house.DelFlag,
                    house.CreateTime,
                    house.CheckId,
                    house.CerName,
                    house.CerCode,
                    house.RegisterRight,
                    house.RegisterBuildingArea,
                    house.ContractSigner,
                    house.ContractRegisterBuildingArea,
                    house.BuildingCode,
                    house.ViolationOriginCode,
                    house.IsBuiltBeforeDate,
                    house.BuiltAreaBeforeDate,
                    house.BuiltAreaAfterDate,
                    house.ViolationBuiltArea,
                    house.DeclareBuildingId
                });
            }

            msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "", JsonHandler.ToJson(objList));
            return JsonHandler.ToJson(msg);
        }
        #endregion

        #region 获取页面编辑数据
        public string GetCheckInfoViewData(int Id)
        {
            JsonMessage msg = new JsonMessage();
            ZS_CheckInfo checkModel = ZS_CheckInfoService.GetModel(Id);
            if (checkModel != null)
            {
                CheckInfoView view = ZS_CheckInfoService.GetEditViewModel(checkModel);
                msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "", JsonHandler.ToJson(view));
            }
            else
            {
                msg = JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "获取数据错误!");
            }
            return JsonHandler.ToJson(msg);
        }
        #endregion
        #region 通过id删除政策性住房
        public string DeleteResettlementInfo(int Id)
        {
            JsonMessage msg = new JsonMessage();
            bool success = ZS_CheckInfoService.DeleteResettlement(Id);
            if (success)
            {
                msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "删除成功!");
            }
            else
            {
                msg = JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "删除失败!");
            }
            return JsonHandler.ToJson(msg);
        }
        #endregion
        #region 通过id删除擅改商业核查情况
        public string DeleteBusinessInfo(int Id)
        {
            JsonMessage msg = new JsonMessage();
            bool success = ZS_CheckInfoService.DeleteBusiness(Id);
            if (success)
            {
                msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "删除成功!");
            }
            else
            {
                msg = JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "删除失败!");
            }
            return JsonHandler.ToJson(msg);
        }
        #endregion
        #region 通过楼栋id获取楼栋基本信息、核查表个数、申报权利人数、已核查面积、该楼栋下的所有核查房屋的情况
        public string StatisticBuildingCheckInfo(int Id)
        {
            JsonMessage msg = new JsonMessage();
            object obj = ZS_CheckInfoService.BuildingCheckInfo(Id);
            if (obj != null)
            {
                msg = JsonHandler.CreateMessage((int)MsgTypeEnum.成功, "", JsonHandler.ToJson(obj));
            }
            else
            {
                msg = JsonHandler.CreateMessage((int)MsgTypeEnum.错误, "获取错误!");
            }
            return JsonHandler.ToJson(msg);
        }

        #endregion


    }
}